// wait_group 实现有条件的等待
#include <workflow/WFFacilities.h>
#include <workflow/WFTaskFactory.h>
#include <workflow/WFHttpServer.h>
#include <wfrest/HttpServer.h>
#include <iostream>
#include <signal.h>
using std::cout;
using std::cerr;
using std::string;
using std::map;
static WFFacilities::WaitGroup waitGroup(1);
void handler(int signum){
    cout << "done\n";
    waitGroup.done();
}

int main(){
    signal(SIGINT,handler);
    // 构造了一个server对象
    wfrest::HttpServer server;
    server.GET("/hello",[](const wfrest::HttpReq * req, wfrest::HttpResp *resp){
        resp->append_output_body("hello");
    });
    server.POST("/world",[](const wfrest::HttpReq * req, wfrest::HttpResp *resp){
        resp->append_output_body("world");
    });
    server.GET("/query",[](const wfrest::HttpReq * req, wfrest::HttpResp *resp){
        // http://192.168.72.128:1234/query?wd=123&content=q
        // 不带&符号的auto 会触发拷贝
        //auto queryMap = req->query_list();
        auto &queryMap = req->query_list();
        for(auto pair:queryMap){
            cout << "first = " << pair.first << " second = " <<pair.second << "\n";
        }
    });
    server.POST("/urlencoded",[](const wfrest::HttpReq * req, wfrest::HttpResp *resp){
        auto & urlencodedMap =  req->form_kv();
        for(auto pair:urlencodedMap){
            cout << "first = " << pair.first << " second = " <<pair.second << "\n";
        }
    });
    server.POST("/form_data",[](const wfrest::HttpReq * req, wfrest::HttpResp *resp){
        auto & formdataMap = req->form();
        for(auto pair:formdataMap){
            cout << "first = " << pair.first
                 << " second.first = "  << pair.second.first
                 << " second.second = " << pair.second.second << "\n";
        }
    });
    server.GET("/upload",[](const wfrest::HttpReq * req, wfrest::HttpResp *resp){
        resp->File("upload.html");
    });
    server.POST("/upload",[](const wfrest::HttpReq * req, wfrest::HttpResp *resp){
        auto & formdataMap = req->form();
        for(auto pair:formdataMap){
            cout << "first = " << pair.first
                 << " second.first = "  << pair.second.first
                 << " second.second = " << pair.second.second << "\n";
        }
    });
    server.POST("/login",[](const wfrest::HttpReq * req, wfrest::HttpResp *resp, SeriesWork * series){
        // http://192.168.72.128:1234/login?username=niqiu&password=123
        auto queryMap = req->query_list();
        string username = queryMap["username"];
        string password = queryMap["password"];
        WFRedisTask * redisTask = WFTaskFactory::create_redis_task(
            "redis://127.0.0.1:6379",10,[username,password,resp](WFRedisTask * redisTask){
                protocol::RedisValue value;
                redisTask->get_resp()->get_result(value);
                if(value.is_string() && value.string_value() == password){
                    resp->String("success");
                }
                else{
                    resp->String("fail");
                }
            }
        );
        redisTask->get_req()->set_request("HGET",{"60user",username});
        series->push_back(redisTask);
    });
    // 开启监听
    if(server.track().start(1234) == 0){ //track 开启追踪
        server.list_routes(); // list_routes 列出支持的方法和路径
        cout << "server start success!\n";
        waitGroup.wait(); //阻塞住主线程
        server.stop(); // 停止监听
    }
    else{
        perror("server start");
        return -1;
    }
    return 0;
}
